<?php
//$_GET['show']={all|pending|accept|deny|self} (default pending)
//$_GET['start']=time start
//$_GET['end']=time end

	include 'include/function.php';
	
function draw_single_request($row,$action) {
	global $current_url;
	echo '<tr>';
		echo '<td align=center>' . user_id_to_name_link($row['user_id']) . '</td>';
		echo "<td align=center>{$row['level']}</td>";
		echo '<td>' . htmlspecialchars($row['reason']) . '</td>';
		echo "<td align=center>{$row['submit_time']}</td>";
		echo '<td align=center>';
			if($action && $row['state']=='pending') {
				echo "<a href='owner_action.php?url=$current_url&action=request_ac&request_id={$row['request_id']}'>Accept</a>&nbsp;";
				echo "<a href='owner_action.php?url=$current_url&action=request_de&request_id={$row['request_id']}'>Deny</a>&nbsp;";
			} else {
				echo $row['state'];
			}
		echo '</td>';
	echo '</tr>';
}

function draw_link($show,$text,$other) {
	global $showrequest_show;
	if($showrequest_show==$show)
		echo "[$text]&nbsp;";
	else
		echo "[<a href='showrequest.php?show=$show$other'>$text</a>]&nbsp;";
}
	show_header(_d('title_requestlist'));
	need_login();
	
	$showrequest_show=str_to_mysql(get_request('show','pending'));

	$request_condition='';
	if(isset($_GET['start'])) {
		$showrequest_start=get_to_mysql($_GET['start']);
		$request_condition=$request_condition . " submit_time >= '$showrequest_start' and ";
	}
	if(isset($_GET['end'])) {
		$showrequest_end=get_to_mysql($_GET['end']);
		$request_condition=$request_condition . " submit_time <= '$showrequest_end' and ";
	}
	
	if($showrequest_show=='self') {
		$query='select * from request_permit where ' . $request_condition
			. " user_id=$_user_id order by node_id asc, request_id asc ";
	} else {
		if($showrequest_show!='all')
			$request_condition = $request_condition . " state='$showrequest_show' and ";

		$query='select * from request_permit where ' . $request_condition
				. '('
					. "node_id in (select node_id from user_permit where (level like '%super%') and (user_id=$_user_id) ) "
					. 'or '
					. "node_id in (select id from node where create_id=$_user_id) "
				. ')'
			. 'order by node_id asc, request_id asc ';
	}
	$ret=oj_query($query);
	$showrequest_last_node=0;
	while($row=mysql_fetch_assoc($ret)) {
		if($showrequest_last_node!=$row['node_id']) {
			if($showrequest_last_node!=0) echo '</table>';
			draw_parents($row['node_id']);
			echo _d('request_list') . '<br>';
			draw_tablelist_head(array(
				_d('request_user'),
				_d('request_level'),
				array(_d('request_reason'),'width="60%"'),
				_d('request_submit_time'),
				_d('request_action')
			));
		}
		draw_single_request($row,$showrequest_show!='self');
		$showrequest_last_node=$row['node_id'];
	}
	if($showrequest_last_node!=0)echo '</table>';
	else echo _d('request_empty') . '<br/>';
	
	$showrequest_url_time='';
	if(isset($showrequest_start)) $showrequest_url_time = $showrequest_url_time . '&start=' . urlencode($showrequest_start);
	if(isset($showrequest_end))   $showrequest_url_time = $showrequest_url_time . '&end='   . urlencode($showrequest_end);
	echo '<center>';
		draw_link('all',	_d('request_all')		,$showrequest_url_time);
		draw_link('accept',	_d('request_accept')	,$showrequest_url_time);
		draw_link('deny',	_d('request_deny')		,$showrequest_url_time);
		draw_link('pending',_d('request_pending')	,$showrequest_url_time);
		draw_link('self',	_d('request_self')		,$showrequest_url_time);
	echo '</center>';

	include 'include/footer.php';
?>

